
function numIslands (grid: string[]): number {

    var toStr = (row: number, col: number) => `${row},${col}`;
    function parseCell(s: string) {
        var xs = s.split(',', 2);
        //var [ row, col, ...tail ] = xs
        return { row: Number.parseInt(xs[0]), col: Number.parseInt(xs[1]) }
    }

    var notVisited = new Set<string>()
    for(let row = 0; row < grid.length; row++) {
        for(let col = 0; col < grid[row].length; col++) {
            notVisited.add( toStr(row, col));
        }
    }

    var islands = 0;

    function exploreIsland(row: number, col: number) {
        var s = toStr( row, col )
        if (row >= 0 && row < grid.length && col >= 0 && col < grid[row].length) {
            
            if (! notVisited.has(s)) return;
            else notVisited.delete(s);

            if (grid[row][col] === '1') {
                exploreIsland(row - 1, col)
                exploreIsland(row + 1, col)
                exploreIsland(row, col - 1)
                exploreIsland(row, col + 1)
            } 
        }
    }

    while(notVisited.size !== 0) {
        var cell = parseCell( notVisited.values().next().value)
        if (grid[cell.row][cell.col] === '1') {
            islands++;
            exploreIsland(cell.row, cell.col);
        }
        else 
            notVisited.delete( toStr(cell.row, cell.col))
    }

    return islands;
};


var input200 = [
    "11110",
    "11010",
    "11000",
    "00000"
]
console.assert( numIslands(input200) == 1)

input200 = [
    "11000",
    "11000",
    "00100",
    "00011"
]
console.assert( numIslands(input200) == 3)

// input200 = [    
//     [ 1, 1, 1, 1, 0],
//     [ 1, 1, 0, 1, 0],
//     [ 1, 1, 0, 0, 0],
//     [ 0, 0, 0, 0, 0]
// ]
// console.assert( numIslands(input200) == 1)

console.log('The END');
